package com.jnsw.testkotlin.mainfragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jnsw.testkotlin.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment1 extends Fragment {
    TextView fr_tv;
    Button mf_btn;
    Subscription subscription;

    Observable<String> observable;
    Subscriber<String> subscriber;

    public MainFragment1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_main_fragment1, container, false);
        fr_tv = (TextView) v.findViewById(R.id.fr_tv);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mf_btn = (Button) v.findViewById(R.id.mf_btn);
//        mf_btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                creatObservable();
//            }
//        });

        RxView.clicks(mf_btn)//mf_btn 添加点击事件监听
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        creatObservable();
                    }
                });


        subscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                fr_tv.append(s);
            }
        };


        return v;
    }

    private void creatObservable() {
        observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("rxandroid----");
                subscriber.onCompleted();
            }
        });
        observable.interval(3000, 3000, TimeUnit.MILLISECONDS);
//      observable.subscribeOn(AndroidSchedulers.mainThread());//
        subscription = observable.subscribe(subscriber);

    }

    @Subscribe
    public void OnReceive(String message) {
        fr_tv.append(message);
        Log.d("message", message);
    }

    @Override
    public void onDestroyView() {
        if (subscription != null) {
            subscription.unsubscribe();
            System.out.println("unsubscribe ----------");
        }
        super.onDestroyView();
    }


}
